package com.codejob.portal.web.action.users;

import java.io.IOException;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.codejob.portal.entity.user.Users;
import com.codejob.portal.services.impl.users.UserManager;
import com.codejob.portal.services.intf.BusinessExcption;
import com.codejob.portal.web.BaseAction;
import com.codejob.portal.web.utils.Struts2Utils;
import com.codejob.portal.web.utils.Token;

@Namespace("/user")
@Results({ @Result(name = "index", location = "../index.jsp", type = "redirect") })
public class UserAction extends BaseAction {

	private Users user; 

	@Autowired
	UserManager userManager;

	private static final long serialVersionUID = 633445360169800367L;

	@Action("/user!prepareRegister")
	public String prepareRegister() {

		return "register";
	}

	public String login() {
		if (user == null || user.getUsername() == null
				|| user.getUsername().equals("") || user.getPassword() == null
				|| user.getPassword().equals("")) {
			return "login";
		}
		try {
			Users loginAble=userManager.doLoign(user);
			if(loginAble!=null){
				Struts2Utils.getSession().setAttribute("user", loginAble);
	    		Object url=Struts2Utils.getSession().getAttribute("unLoginUrl");
	    		if(url==null){
	    			return "index";
	    		}
	    		String path=(String)url;
	    		Struts2Utils.getSession().setAttribute("unLoginUrl", null);
	    		Struts2Utils.getResponse().sendRedirect(path);
				return null;
			}else{
				return "login";
			}
		} catch (BusinessExcption e) {
			//e.printStackTrace();
		} catch (IOException e) {
			//e.printStackTrace();
		}
		return "login";
	}
	
	public String loginAjax() {
		if (user == null || user.getUsername() == null
				|| user.getUsername().equals("") || user.getPassword() == null
				|| user.getPassword().equals("")) {
			Struts2Utils.renderText("false");
			return null;
		}
		try {
			Users loginAble=userManager.doLoign(user);
			if(loginAble!=null){
				Struts2Utils.getSession().setAttribute("user", loginAble);
				Struts2Utils.renderText("true");
	    		return null;
			}else{
				Struts2Utils.renderText("false");
				return null;
			}
		} catch (BusinessExcption e) {
			//e.printStackTrace();
			Struts2Utils.renderText("false");
		} 
		return null;
	}

	@Action(value = "/user!register")
	@Token
	public String register() {
		try {
			userManager.register(user);
			Struts2Utils.getSession().setAttribute("user", user);
		} catch (BusinessExcption e) {
			String anErrorMessage = this.getText("portal.user.regError");
			this.addActionError(anErrorMessage);
			return prepareRegister();

		} catch (Exception e) {
			e.printStackTrace();
			return prepareRegister();
		}
		return "regSuccess";
	}

	@Token
	public String popRegister() {
		try {
			userManager.register(user);
			Struts2Utils.getSession().setAttribute("user", user);
		} catch (BusinessExcption e) {
			String anErrorMessage = this.getText("portal.user.regError");
			this.addActionError(anErrorMessage);
			return prepareRegister();
		} catch (Exception e) {
			e.printStackTrace();
			return prepareRegister();
		}
		return "index";
	}

	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	public String checkUsernameAjax() {
		try {
			boolean isExist = userManager.checkUsername(user);
			if (!isExist) {
				Struts2Utils.renderText("true");
			} else {
				Struts2Utils.renderText("false");
			}
		} catch (BusinessExcption e) {
			Struts2Utils.renderText("false");
			e.printStackTrace();
		}

		return null;
	}

}
